package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.api.core.util.JwtTokenUtil;
import cn.stylefeng.guns.modular.business.model.params.BatchOrderParam;
import cn.stylefeng.guns.modular.business.model.params.BatchOrderQueryParam;
import cn.stylefeng.guns.modular.business.model.result.BatchOrderResult;
import cn.stylefeng.guns.modular.business.service.BatchOrderService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 订单表控制器
 *
 * @author jwj
 * @Date 2019-07-12 14:20:25
 */
@RestController
@RequestMapping("/gunsApi/batchOrder")
public class BatchOrderApi extends BaseController {

    private String apiSign = "pmy2019app";

    @Autowired
    private BatchOrderService batchOrderService;

    @ApiOperation("批量提交")
    @PostMapping("create")
    public ResponseData create(@RequestBody BatchOrderParam batchOrderParam) {
        return ResponseData.success(batchOrderService.add(batchOrderParam));
    }

    /**
     * 获取礼品卡所有分类
     *
     * @return
     */
    @ApiOperation("开始处理批次")
    @PostMapping("to-orders")
    public ResponseData getCardList(@RequestBody BatchOrderParam batchOrderParam) {
        if (!batchOrderParam.getApiSign().equals(apiSign)) {
            return ResponseData.error(700, "错误");
        }
        return ResponseData.success(batchOrderService.processOrder(batchOrderParam));
    }

    /**
     * 按条件查询我的订单
     *
     * @param request
     * @param batchOrderQueryParam
     * @return
     */
    @ApiOperation("按条件分页查询我的批量订单")
    @PostMapping("/get-Orders")
    public ResponseData getMyOrders(HttpServletRequest request, @RequestBody BatchOrderQueryParam batchOrderQueryParam) {
        String authorization = request.getHeader("Authorization");
        String[] token = authorization.split(" ");
        Long userId = Long.valueOf(JwtTokenUtil.getUsernameFromToken(token[1]));
        batchOrderQueryParam.setCardOwnerId(userId);
        if (batchOrderQueryParam.getCurrent() == null) {
            return ResponseData.error(700, "分页参数错误");
        }

        if (batchOrderQueryParam.getSize() == null) {
            return ResponseData.error(700, "分页参数错误");
        }
        Page page = new Page(batchOrderQueryParam.getCurrent(), batchOrderQueryParam.getSize());

        if (StringUtils.isNotEmpty(batchOrderQueryParam.getStartTime())) {
            batchOrderQueryParam.setStartTime(batchOrderQueryParam.getStartTime() + " 00:00:00");
            batchOrderQueryParam.setEndTime(batchOrderQueryParam.getEndTime() + " 23:59:59");
        }
        Page<BatchOrderResult> myOrders = batchOrderService.getMyBatchOrders(page, batchOrderQueryParam);
        return ResponseData.success(myOrders);
    }

}


